//
//  BaseWebViewController.m
//  DaYing
//
//  Created by DaYing  on 2023.
//  Copyright © 2023年 DaYing . All rights reserved.
//

#import "BaseWebViewController.h"
#import <WebKit/WebKit.h>

@interface BaseWebViewController () <WKNavigationDelegate>

//webview,加载网页用。
@property (strong, nonatomic) WKWebView *webView;

@end

@implementation BaseWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    if (_webTitle.length > 0) {
        [self setNavigationItemTitleViewWithTitle:kSafeString(_webTitle) color:UIColorFromHex(0x333333)];
    } else {
        [self setNavigationItemTitleViewWithTitle:self.getUrlTitle color:UIColorFromHex(0x333333)];
    }
    self.view.backgroundColor = [UIColor whiteColor];

    [SVProgressHUD show];
    
    WKWebViewConfiguration *webConfiguration = [WKWebViewConfiguration new];
    _webView = [[WKWebView alloc] initWithFrame:CGRectMake(15, self.navigationBarHeight + 15, kScreenWidth - 15*2, kScreenHeight - self.navigationBarHeight - 15*2 - self.bottomBarHeight) configuration:webConfiguration];
    _webView.navigationDelegate = self;
    [_webView setOpaque:NO];
    [self.view addSubview:_webView];
    NSString *urlStr = _url;
    NSURL *url = [NSURL URLWithString:urlStr];
    if ([_type isEqualToString:@"file"]) {
        url = [NSURL fileURLWithPath:urlStr];
    }
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
    [_webView loadRequest:request];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}

- (void)showBack {
    NSString *imageName = @"black_back";
    [self setLeftItemWithIcon:[UIImage imageNamed:imageName] selector:@selector(backAction:)];
}

- (void)setUrl:(NSString *)url {
    if ([_type isEqualToString:@"file"]) {
        _url = url;
    } else {
        if (![url containsString:@"http"] && ![url containsString:@"file://"]) {
            _url = [NSString stringWithFormat:@"http://%@",url];
        } else {
            _url = url;
        }
    }
}

- (NSString *)getUrlTitle {
    NSString *htmlString = [NSString stringWithContentsOfURL:[NSURL URLWithString:kSafeString(_url)] encoding:NSUTF8StringEncoding error:nil];
    if (htmlString.length > 0) {
        NSString *str = @"<title>";
        NSRange range = [htmlString rangeOfString:str];
        if(range.location != NSNotFound) {
            htmlString = [htmlString substringFromIndex:range.location + str.length];
        }

        range = [htmlString rangeOfString:@"</title>"];
        if(range.location != NSNotFound) {
            htmlString = [htmlString substringToIndex:range.location];
        }
    }
    return kSafeString(htmlString);
}

- (void)backAction:(UIButton *)sender {
    if (self.webView.canGoBack) {
        [self.webView goBack];
    } else {
        [SVProgressHUD dismiss];
        [self dismissViewControllerAnimated:YES completion:nil];
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (nullable WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures{
    if (navigationAction.request.URL) {
        WKWebView *wkwebView = [[WKWebView alloc] initWithFrame:webView.frame configuration:configuration];
        [wkwebView loadRequest:navigationAction.request];
        return wkwebView;
    }
    return nil;
}

// 页面加载完成之后调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    [SVProgressHUD dismiss];
}

@end

